package ca.cbc.msb.mediacms;

import java.util.List;

import javax.jms.JMSException;
import javax.naming.NamingException;

import ca.cbc.msb.entity.MSBMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ca.cbc.msb.entity.MSBNotification;
import ca.cbc.msb.enums.QueueEnum;
import ca.cbc.msb.mq.MessageUtil;
import ca.cbc.msb.mq.MessageUtilImpl;
import ca.cbc.msb.util.AppConfig;

public class NewReleaseNotificationHandler implements NotificationHandler {
	private static Logger logger = LoggerFactory.getLogger(NewReleaseNotificationHandler.class);
	private static final String filterParamString = "filter=Release|post";
	
	//private long newReleaseSequenceId;
	private String sequenceFile = AppConfig.getProperty("nh.newrelease.sequenceid.filename");

    //@Inject Could not inject yet, we are not getting it from Guice!
    private MessageUtil messageUtil;
	public NewReleaseNotificationHandler()
	{
        try {
            messageUtil = new MessageUtilImpl();
        } catch (NamingException e) {
            logger.error("Error in creating message util", e);
            throw new RuntimeException(e);
        } catch (JMSException e) {
            logger.error("Error in creating message util", e);
            throw new RuntimeException(e);
        }

	}
	
	@Override
	public void handleMessage(List<MSBNotification> nList) throws NamingException, JMSException
	{    
        logger.debug("-----Pushing to ReleaseNotificationQueue-----");
        for(MSBNotification n : nList)
        {
            MSBMessage message = new MSBMessage();
            message.setNotification(n);
        	messageUtil.submitMessage(QueueEnum.ReleaseNotificationQueue, message);
            logger.debug(n.toString());
            messageUtil.commit();
        }

	}
	
	@Override
	public String getFilterParam() {
		return filterParamString;
	}

    @Override
    public String getSequenceFileName() {
        return sequenceFile;
    }

    @Override
    public void cleanUp() {
         logger.info("closing the jms session for New Release Notification Handler");
        messageUtil.disconnect();
    }
}
